package com.easy.controller;

import com.easy.bean.Cases;
import com.easy.bean.DepIdCount;
import com.easy.common.CommonResult;
import com.easy.dao.ICasesDao;
import com.easy.service.IBedService;
import com.easy.service.ICasesService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController

public class CasesController {

    @Autowired
    ICasesDao dao;
    @Autowired
    ICasesService caseS;
    @Autowired
    IBedService bedS;


    @GetMapping("cases")
    public CommonResult getCases(Cases cases, Page page){
        List<Cases> list=caseS.getCases(cases,page);

        int count=caseS.getCount(cases);
        HashMap map = new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);
    }


    @PostMapping("cases")
    public CommonResult addCasesJSON(@RequestBody Cases cases){
        caseS.addCases(cases);
        cases=caseS.getCasesById(cases.getId());
       return CommonResult.success(cases);
    }
    @DeleteMapping("cases/{id}")
    public CommonResult delCases(@PathVariable int id){
        int count=dao.delCases(id);
        if(count>0){
            return CommonResult.success();
        }else {
            return CommonResult.fail();
        }
    }
    @PutMapping("casesjson")
    public CommonResult editCases(@RequestBody Cases cases){

        System.out.println(cases);
        try {
            caseS.editCases(cases);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.fail(0,e.getMessage());
        }
        cases=caseS.getCasesById(cases.getId());
        System.out.println(cases);
        return CommonResult.success(cases);
    }


    @GetMapping("getDep_idCount")
    public CommonResult getDep_idCount(){
        List<DepIdCount> list =caseS.getDep_idCount();
        for (DepIdCount o:list){
            System.out.println(o);
        }
        return CommonResult.success(list);
    }




}
